//index.js
//获取应用实例
const app = getApp();
let pNo = 1, gId = '', ris = true;
const getToken = require('../../utils/get_token.js');
const formatTime = require('../../utils/util.js');

let openId = '', memberId = '';
let cookie, token;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  getClass(that);
  indexList(that)//初始加载
}
const getClass = function (that) {//分类
  let params = ['shop/pptIco/lorB', {"A":"SLP_TCP_FL"}]
  getToken.getData(function (res) {
    that.data.fList = res.data.A.SLP_TCP_FL;
    for (let i = 0; i < that.data.fList.length; i++) {
      if (gId == that.data.fList[i].K) {
        that.data.fList[i].show = true;
      } else {
        that.data.fList[i].show = false;
      }
    }
    that.setData({
      fList: that.data.fList
    })
  }, ...params)
}
const indexList = (that) => {//商品列表
  if (that.data.boolT) {
    that.setData({hidden: false})
    that.data.boolT = false;
    let params = ['shop/pingTuan/index', {"type":2,"pageNo": pNo,"gId": gId,"orderBy":that.data.orderBy}]
    getToken.getData(function (res) {
      let data = res.data;
      if (data.result.length > 0) {
        for (var i = 0; i < data.result.length; i++) {
          data.result[i].goodsLabelList = data.result[i].goodsLabelList || [];
          data.result[i].goodsPinTuanVo.intPinPrice = (data.result[i].goodsPinTuanVo.list[0].intPrice / 100).toFixed(2);
          data.result[i].goodsPinTuanVo.intPrice = (data.result[i].goodsPinTuanVo.intPrice / 100).toFixed(2);
          that.data.list.push(data.result[i]);
        }
        pNo++;
      }
      setTimeout(function () {
        that.data.boolT = true;
        that.setData({
          list: that.data.list,
          hidden: true
        })
      }, 500)
    }, ...params)
  }
};

Page({
  data: {
    fList: [],
    orderBy:'pingCount',
    hidden: false,
    boolT: true,
    hList: [
      { 'name': '正在开团', 'show': false, 'triangle': false, "type": 'one', 'orderBy': 'tuanNum' },
      { 'name': '销量', 'show': true, 'triangle': false, "type": 'one', 'orderBy': 'pingCount' },
      { 'name': '价格', 'show': false, 'triangle': true, "type": 'two', 'orderBy': 'priceDesc' }],
    list: []
  },
  onLoad: function (options) {
    let that = this;
    pNo = 1;
    gId = options.id;
    memberId = wx.getStorageSync('memberId') || '';
    openId = wx.getStorageSync('openId') || '';
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    ckToken(that);
  },
  onShow: function () {
  },
  changeD:function(e){//切换下面
      let that = this;
    if(that.data.boolT){
      that.setData({hidden: false})
      that.data.boolT = false;
      let type = e.currentTarget.dataset.type;
      let name = e.currentTarget.dataset.name;
      let order = e.currentTarget.dataset.order;
      pNo = 1;
      that.data.list = [];
      for (let i = 0; i < that.data.hList.length; i++) {
        if (name == that.data.hList[i].name) {
          that.data.hList[i].show = true;
        } else {
          that.data.hList[i].show = false;
        }
      }
      if (type == 'one') {
        that.data.orderBy = order
      } else {
        if (ris)//高到低
        {
          that.data.orderBy = 'priceDesc'
          ris = false
        } else {
          that.data.orderBy = 'priceAsc'
          ris = true
        }
      }
      that.setData({
        hList: that.data.hList,
        orderBy: that.data.orderBy
      })
      let params = ['shop/pingTuan/index', {"type":2,"pageNo": pNo,"gId": gId,"orderBy":that.data.orderBy}]
      getToken.getData(function (res) {
        let data = res.data;
        if (data.result.length > 0) {
          for (var i = 0; i < data.result.length; i++) {
            data.result[i].goodsLabelList = data.result[i].goodsLabelList || [];
            data.result[i].goodsPinTuanVo.intPinPrice = (data.result[i].goodsPinTuanVo.list[0].intPrice / 100).toFixed(2);
            data.result[i].goodsPinTuanVo.intPrice = (data.result[i].goodsPinTuanVo.intPrice / 100).toFixed(2);
            that.data.list.push(data.result[i]);
          }
          pNo++;
        }
        setTimeout(function () {
          that.data.boolT = true;
          that.setData({
            list: that.data.list,
            hidden: true
          })
        }, 500)
      }, ...params)
    }
  },
  changeC: function (e) {//切换上面
    let that = this;
    if(that.data.boolT){
      that.setData({hidden: false})
      that.data.boolT = false;
      gId = e.currentTarget.dataset.id;
      pNo = 1;
      that.data.list = [];
      for (let i = 0; i < that.data.fList.length; i++) {
        if (gId == that.data.fList[i].K) {
          that.data.fList[i].show = true;
        } else {
          that.data.fList[i].show = false;
        }
      }
      that.setData({fList: that.data.fList})
      let params = ['shop/pingTuan/index', {"type":2,"pageNo": pNo,"gId": gId,"orderBy":that.data.orderBy}]
      getToken.getData(function (res) {
        let data = res.data;
        if (data.result.length > 0) {
          for (var i = 0; i < data.result.length; i++) {
            data.result[i].goodsLabelList = data.result[i].goodsLabelList || [];
            data.result[i].goodsPinTuanVo.intPinPrice = (data.result[i].goodsPinTuanVo.list[0].intPrice / 100).toFixed(2);
            data.result[i].goodsPinTuanVo.intPrice = (data.result[i].goodsPinTuanVo.intPrice / 100).toFixed(2);
            that.data.list.push(data.result[i]);
          }
          pNo++;
        }
        setTimeout(function () {
          that.data.boolT = true;
          that.setData({
            list: that.data.list,
            hidden: true
          })
        }, 500)
      }, ...params)
    }
  },
  enter: function (e) {
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../paired_detail/paired_detail?scene=' + e.currentTarget.dataset.id,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  go_detail: function (e) {//直接去详情 
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../paired_detail/paired_detail?scene=' + e.currentTarget.dataset.id,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  },
  onReachBottom: function () {//上啦加载更多
    let that = this;
    indexList(that);
  },
  onPullDownRefresh: function (e) {//下拉刷新
    let that = this;
    wx.showNavigationBarLoading()//加载动画
    pNo = 1;
    that.data.list = [];
    indexList(that);
    setTimeout(function () {
      wx.hideNavigationBarLoading()//隐藏动画
      wx.stopPullDownRefresh()//停止加载
    }, 500)
  }
})
